package com.wenwei.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wenwei.adapter.LookSuAdapter;
import com.wenwei.adapter.ParkingRecordAdapter;
import com.wenwei.bean.ParkingHistoryBean;
import com.wenwei.net.ApiManager;
import com.wenwei.net.BaseSubscriber;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.Constant;
import com.wenwei.parking.R;
import com.wenwei.utils.UserStringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/21.
 */
public class ParkingRecordActivity extends BaseActivity implements ParkingRecordAdapter.CallBack{
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.base_save_tv)
    TextView baseSaveTv;
    @Bind(R.id.recycler)
    XRecyclerView recycler;

    @Bind(R.id.empty)
    View mView;
    ParkingRecordAdapter mAdapter;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_parking_record;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("停车记录");
        baseSaveTv.setText("开发票");
        baseSaveTv.setVisibility(View.VISIBLE);
        recycler.setEmptyView(mView);
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                recycler.setLoadingMoreEnabled(true);
                getParkHistory(1);
            }

            @Override
            public void onLoadMore() {
                if (currentPage < pageTotalCount) {
                    currentPage = currentPage + 1;
                    recycler.setLoadingMoreEnabled(true);
                    getParkHistory(2);
                } else {
                    recycler.setLoadingMoreEnabled(false);
                    recycler.loadMoreComplete();
                }
            }
        });
        mAdapter = new ParkingRecordAdapter();
        mAdapter.setmCallBack(this);
        recycler.setAdapter(mAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.base_back, R.id.base_save_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.base_save_tv:
                goActivity(mContext, InvoiceActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        getParkHistory(1);
    }

    private int currentPage = 1;//当前分页
    private int pageTotalCount;//分页总数
    private List<ParkingHistoryBean.DataBean> mList = new ArrayList<>();
    private BaseSubscriber<ParkingHistoryBean> hisSb;
    private void getParkHistory(final int type){
        hisSb = new BaseSubscriber<ParkingHistoryBean>(mContext) {
            @Override
            public void onSuccess(ParkingHistoryBean parkingHistoryBean) {
                pageTotalCount = parkingHistoryBean.getTotal_page();

                if(type == Constant.LOAD_TYPE_REFRESH){
                    mList.clear();
                    mList.addAll(parkingHistoryBean.getData());
                    mAdapter.setmList(mList);
                    recycler.refreshComplete();
                }else{
                    mList.addAll(parkingHistoryBean.getData());
                    mAdapter.setmList(mList);
                    recycler.loadMoreComplete();
                }

                mAdapter.notifyDataSetChanged();
            }
        };
        ApiManager.getInstance().getParkHistory(hisSb, UserStringUtils.getUserId(mContext),currentPage);
    }

    @Override
    public void onItemClick(ParkingHistoryBean.DataBean dataBean) {
        Bundle bundle = new Bundle();
        bundle.putInt("orderId",dataBean.getOrderId());
        if(dataBean.getIsAllege() == 0){
            goActivity(mContext,PaySuccessSuActivity.class,bundle,99);
        }else {
            goActivity(mContext,LoookSuActivity.class,bundle);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if (requestCode == 99 ){
                getParkHistory(1);
            }
        }
    }
}
